<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<%
    String path = request.getContextPath();
    String basePath = request.getServerName() + ":"
            + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>队伍排行</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../include/commonFile.jsp" %>
</head><body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <div class="layui-tab layui-tab-brief mt20">
                <ul class="layui-tab-title">
                    <li id="总排行">总排行</li>
                    <li class="layui-this" id="groupRank">队伍排行</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item  layui-show">
                        <form class="layui-form" action="${ctx}/competition/result/groupRank.do?projectId=${project.id}" id="myForm" method="post">
                            <input type="hidden" name="pageNo" id="pageNo"/>
                            <div class="f-search-bar">
                                <div class="search-container">
                                    <ul class="search-form-content">
                                        <li class="form-item-inline"><label class="search-form-lable">赛程</label>
                                            <div class="layui-input-inline">
                                                <select name="scheduleId">
                                                    <option value="">总赛程</option>
                                                    <c:forEach var="schedule" items="${schedules}">
                                                        <option value="${schedule.id}" ${rankInput.scheduleId == schedule.id ? 'selected="selected"' : ''}><fmt:formatDate value="${schedule.playDay}" pattern="yyyy年MM月dd日"/></option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </li>
                                        <li class="form-item-inline"><label class="search-form-lable">队伍</label>
                                            <div class="layui-input-inline">
                                                <select name="groupId">
                                                    <option value="">全部</option>
                                                    <c:forEach var="group" items="${groups}">
                                                        <option value="${group.id}" ${rankInput.groupId == group.id ? 'selected="selected"' : ''}>${group.groupName}</option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </li>
                                        <li class="form-item-inline">
                                            <div class="sub-btns">
                                                <a class="layui-btn layui-btn-danger" href="javascript:submitFunction('#myForm')">查询</a>
                                                <a class="layui-btn layui-btn-normal" href="javascript:resetFunction('#myForm')">重置</a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </form>
                        <div class="my-act-list-content">
                            <ul class="num">
                                <div class="r">
                                    <li style="cursor: pointer;" class="r">
                                        <a class="layui-btn layui-btn-danger layui-btn-sm" id="resultSetBtn">设置队伍排行</a>
                                        <a class="layui-btn layui-btn-danger layui-btn-sm" id="exportExcelBtn">导出EXCEL</a>
                                        <a class="layui-btn layui-btn-danger layui-btn-sm" id="downloadTemplateBtn">下载团队成绩导入模板</a>
                                        <a class="layui-btn layui-btn-danger layui-btn-sm" id="importResultBtn">导入团队成绩EXCEL</a>
                                        <form id="userForm" method="post" class="dn" action="${ctx}/competition/groupResult/groupResultImport.do" enctype="multipart/form-data">
                                            <input type="file" name="uploadFile" id="upload_input" single
                                                   accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                                            <input type="hidden" name="projectId" value="${project.id}"/>
                                        </form>
                                    </li>
                                </div>
                                <p class="cl"></p>
                            </ul>
                            <div class="cl">
                                <table class="layui-table">
                                    <colgroup>
                                        <col width="60px">
                                        <col>
                                        <col>
                                        <col>
                                        <col>
                                        <col>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>排名</th>
                                        <th>队伍名称</th>
                                        <th>成员数</th>
                                        <th>比赛成绩</th>
                                        <th>里程</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach var="memberResult" items="${memberResults}" varStatus="status">
                                        <tr>
                                            <td><fmt:formatNumber value="${memberResult.rowno}" maxFractionDigits="0"></fmt:formatNumber></td>
                                            <td>${memberResult.groupName}</td>
                                            <td>${memberResult.memberCount}</td>
                                            <td>${empty memberResult.result ? '00:00:00' : memberResult.result}</td>
                                            <td>${empty memberResult.distance ? '0' : memberResult.distance}</td>
                                            <td class="opts-btns tb-opts">
                                                <div class="comm-opts">
                                                    <a href="${ctx}/competition/result/groupPersonalRank.do?projectId=${project.id}&groupId=${memberResult.groupId}">查看组内人员排名</a>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                    </tbody>
                                </table>
                                <c:if test="${page.totalCount == 0}">
                                    <div class="f16 tc mt15">还没有队伍</div>
                                </c:if>
                                <div class="page-content">
                                    <div id="page_content" class="page-container"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-tab-item"></div>
                </div>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
</div>

<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">
    showActive('${input.createStart}', '${input.createEnd}', '#timeType');
    var myLoadIndex;
    $(function () {

        //加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        layui.use(['element'], function () {
            var element = layui.element;
            element.on('tab', function (data) {
                if (data.index == 0) {
                    window.location.href = "${ctx}/competition/result/personalRank.do?projectId=${project.id}";
                } else if (data.index == 1) {
                    window.location.href = "${ctx}/competition/result/groupRank.do?projectId=${project.id}";
                }
            });
        });
    })

    function exportData() {
        layer.confirm('确认要导出Excel吗?', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            var url = "${ctx}/competition/result/exportAllGroupResult.do?projectId=${project.id}";
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            $.post(url, $("#myForm").serialize(), function (res) {
                top.layer.close(loadIndex);
                if (res.success) {
                    window.location.href = res.data;
                } else {
                    util.layerMsgError(res.description)
                }
            });
            top.layer.close(index);
        });
    }

</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '我发布的项目'
        },{
            name: '${project.title}'
        },{
            name: '数据查看',
            curr: true
        }],
        btns:[{
            name: '返回',
            icon: 'back',
            href: '${ctx}/competition/project/list.do'
        }]
    });

    $(function () {
        var isRepeat = false;
        $.ajax({
            type: 'get',
            async: false, // 使用同步的方法
            data: {},
            dataType: 'json',
            success: function (res) {
                isRepeat = res.data.isExpire;
            },
            url: '${ctx}/system/member/expireCheck.do'
        });

        var importResultBtn = document.getElementById("importResultBtn");
        var upload_input = document.getElementById("upload_input");

        var resultSetBtn = document.getElementById("resultSetBtn");
        var exportExcelBtn = document.getElementById("exportExcelBtn");
        var downloadTemplateBtn = document.getElementById("downloadTemplateBtn");

        // 设置成绩
        resultSetBtn.addEventListener('click', function (e) {
            if (!isRepeat) {
                openDialogNoButton('设置队伍排行','${ctx}/competition/groupResult/toForm.do?projectId=${project.id}','880px','600px')
            } else {
                txz.openExpireDialog();
            }
            e.preventDefault();
        });

        // 导出Excel
        exportExcelBtn.addEventListener('click', function (e) {
            if (!isRepeat) {
                exportData();
            } else {
                txz.openExpireDialog();
            }
            e.preventDefault();
        });

        // 下载模板
        downloadTemplateBtn.addEventListener('click', function (e) {
            if (!isRepeat) {
                window.location.href = '${ctx}/competition/result/createTemplate.do?type=2&projectId=${project.id}';
            } else {
                txz.openExpireDialog();
            }
            e.preventDefault();
        });

        // 导入成绩
        importResultBtn.addEventListener('click', function (e) {
            console.log('expireCheck');
            if (!isRepeat) {
                upload_input.click();
            } else {
                txz.openExpireDialog();
            }
            e.preventDefault();
        }, false);

        upload_input.addEventListener('change', function (e) {
            console.log(e.target.files[0].name);
            //loading层
            myLoadIndex = layer.msg('努力导入中,请耐心等待...', {
                icon: 16,
                shade: 0.1,
                time: 0
            });

            var formData = new FormData();
            formData.append("uploadFile",$("#upload_input")[0].files[0]);
            formData.append("projectId", '${project.id}');

            $.ajax({
                url: $("#userForm").attr('action'),
                type: 'post',
                data: formData,
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function () {
                    console.log("正在进行，请稍候");
                },
                success: function (res) {
                    console.info('执行完了');
                    $("#userForm")[0].reset();
                },
                error: function (res) {
                    $("#userForm")[0].reset();
                    util.layerMsgError("上传异常：" + JSON.stringify(res));
                }
            });
            e.preventDefault();
        }, false);

        util.initWebSocket(function (event) {
            var jsonObj = JSON.parse(event.data);
            var totalNum = jsonObj.totalNum;
            var currentNum = jsonObj.currentNum;
            var successNum = jsonObj.successNum;
            var errorNum = jsonObj.errorNum;
            var resourceUrl = jsonObj.resourceUrl;
            var isComplete = jsonObj.isComplete;
            if (totalNum == currentNum && isComplete) {
                var message = "一共" + totalNum + "条数据；成功" + successNum + "条；错误" + errorNum + "条。";
                util.layerMsgSuccess(message, function () {
                    if (util.isValid(resourceUrl)) {
                        location.href = resourceUrl;
                        setTimeout(function () {
                            location.reload();
                        }, 500);
                    } else {
                        location.reload();
                    }
                })
            } else {
                var message = "一共有" + totalNum + "条数据，当前第" + currentNum + "条。";
                var length = $(".layui-layer-content.layui-layer-padding").length;
                if (length == 0) {
                    myLoadIndex = layer.msg('<i class="layui-layer-ico layui-layer-ico16"></i>' + message, {
                        icon: 16,
                        shade: 0.1,
                        time: 0
                    });
                } else {
                    $(".layui-layer-content.layui-layer-padding").html('<i class="layui-layer-ico layui-layer-ico16"></i>' + message);
                }
            }
        })
    });
</script>
</body>
</html>